Skip to content

Reimplement 8 Skirmish and mission functions at 100% from #525, plus 2 with register allocation differences - #557

Open
gynt wants to merge 19 commits into
sourcehold:mainfrom
gynt:reimpl/pr525-matched
Open

gynt wants to merge 19 commits into
sourcehold:mainfrom
gynt:reimpl/pr525-matched

Conversation

@gynt

@gynt gynt commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

This PR takes ten functions from draft #525 (reimpl/mission-skirmish-flow):

  • the eight that are at 100%;
  • two where only register allocation differs, under the rule that the remaining differences change no behaviour.

Their .cpp files come from the draft, with a style pass. The draft's other functions stay in #525.

Measured on origin/main:

  • I measured the eight 100% functions on 46f21c5. For those builds I patched the dword/int mismatch in findBestAttackTileByPathCost.cpp locally.
  • Main has since fixed that mismatch in 592a17d, and the branch now merges that commit.
  • I then built 592a17d with all ten files in the build list. IO/ResourceManager/paths.cpp was also in the list, as a callee needed for the link. It is not part of this PR.
  • After merging main 0701799, a third full build with both resolver sets gives the same numbers for all ten functions.
  • All builds had the struct resolvers and the function resolvers switched on, and I ran reccmp after each. The numbers below are from the build on 592a17d.
Function Address raw % normalized % Status remark
Skirmish::SkirmishLeaderboard_DeleteEntry 0x004D5370 100.0 100.0 100.0% | Reimplemented
Skirmish::StoreGameIntoSKMasters 0x004D52A0 100.0 100.0 100.0% | Reimplemented
Skirmish::Skirmish_PrepareLeaderboardView 0x004D9400 99.1 97.6 100.0% | Reimplemented (counted: only the labels of two array[-1] addresses differ)
Game::Tutorial_IsActionAllowed 0x004BD800 100.0 100.0 100.0% | Reimplemented
MapPropertiesState::adjustEventMonthAndYearForSection1047 0x004BC110 100.0 100.0 100.0% | Reimplemented
MapPropertiesState::loadMapSiegeHeaderForMissionIndex 0x004C6820 87.0 100.0 100.0% | Reimplemented
MapPropertiesState::loadMissionMapAndSetLord 0x004C6880 100.0 100.0 100.0% | Reimplemented
Synchrony::LoadSkirmishCampaignData 0x004C6B20 100.0 100.0 100.0% | Reimplemented
SkirmishLobbySetupStructure::restoreSkirmishLobbySetup 0x00490060 99.3 95.2 99.3% | Register allocation differs
MenuTextInputState::loadOrSaveGame 0x004968A0 93.1 93.5 93.1% | Register allocation differs
  • Skirmish_PrepareLeaderboardView: the instructions, operands and offsets are the same as the original's. Two addresses just before an array differ only in the symbol name that reccmp gives them:

    • INT_ARRAY_00eb0e48 - 4 in the backwards loop;
    • INT_ARRAY_00ed2be0 - 4 in the date column.

    The original calls these I<char>::instance and I<int>::instance. Ours calls them a resolver instance and a resolver initializer.

  • loadMapSiegeHeaderForMissionIndex: the raw difference is only the relative offsets of its four tail jumps to loadMapSiegeHeaderSections (0x004C3110).

Register allocation only (no change in behaviour):

  • restoreSkirmishLobbySetup: the only difference is the operand order of one byte store. The original has mov byte ptr [eax + esi], dl and ours has [esi + eax], which is the same address.
  • loadOrSaveGame: the original keeps the constant 16 in ebp. It uses ebp for both std::string capacity checks and for the fileListVisibleRowCount store; ours uses the immediate each time. The other differences follow from that:
    • the longer immediate encodings shift the relative jump offsets;
    • the loop's alignment filler is a jmp over padding instead of lea esp, [esp];
    • the call and EH-handler targets differ only as labels.

Left in #525: each of these functions has a difference that the rule does not allow.

Function raw % Difference
ComputePlayerRanking 98.1 The original loads through a pointer and then compares. Ours folds an indexed load into the cmp, which is instruction selection.
spawnInvasionEventAttackWave 91.0 Ours compiles to a jump table; the original uses a chain of compares.
computeMissionCompletionScore 88.7 Tail merging and block layout differ.
SetupSkirmishMode 82.6 Block layout differs.
loadMap 79.0 Store order differs.
processSingleplayerEvents 64.0 Frame size and layout differ.

Style pass (from REIMPLEMENTATION_STYLE_GUIDE.md on main), done in the per-function commits:

  • The class's own .func.hpp is the first include, and unused includes and usings are gone.
  • ++ / -= replace self-assignments, and hex literals are decimal.
  • Early returns replace nested ifs.
  • countof / sizeof replace the literals 250, 249, 0x8ca and 80000.
  • In restoreSkirmishLobbySetup, for loops replace the 45 unrolled copies. MSVC unrolls them the same way as the original.
  • strcpy replaces the two hand-written copy loops.
  • Named locals replace iVarN / pCVar9, and the old Ghidra comments are rewritten.

Pre-review pass (the review: commit):

  • Enum values replace numbers: LeaderBoardFilter / LeaderBoardSortColumn, TutorialActionType with BuildingType, and MenuModalType for loadOrSaveGame's action.
  • TRUE / FALSE replace 0/1 on flag fields.
  • countof gives the loop bounds in LoadSkirmishCampaignData, and its narrowing casts are gone.
  • The save patterns come from string-macros.hpp.
  • Each of the three files that use strcpy starts with #pragma warning(disable : 4996).
  • Unused includes are gone.

Some forms stay only because they give the match: the <= countof - 1 test, the do-while loops, the repeated branches and the signed cast. The PR comment lists what each readable form costs, and the header-side issues I left alone. Two fixme: comments stay in the code:

  • loadMapSiegeHeaderForMissionIndex: the header declares the mission number as char*, though it is an int.
  • loadOrSaveGame: the original writes DAT_ArrayOfMapIndices2[i - 1], so i = 0 writes one entry before the array.

After the pass and clang-format, the full build and reccmp give the numbers in the table above.

Header changes: none. The three enum headers this PR used are on main now, identical, through 0701799 ("upstream ghidra changes"), and the branch merges it:

  • Game/Skirmish/Enums/LeaderBoardFilter.hpp: LBF_ALL_GAMES, LBF_CRUSADER_TRAIL_ONLY, LBF_CUSTOM_GAMES_ONLY, after the ranking buttons that set them.
  • Game/Skirmish/Enums/LeaderBoardSortColumn.hpp: LBSC_UNKNOWN_0, LBSC_UNKNOWN_1, LBSC_MISSION, LBSC_DURATION, LBSC_DATE.
  • Game/Enums/TutorialActionType.hpp: TAT_SELECT_BUILDING, TAT_UNKNOWN_2, TAT_CHANGE_RATIONS, TAT_CHANGE_TAXES, after the callers on main.

GL / visibility entries: none needed. The draft's only cmake difference is the setDestinationForUnit line that main has since removed in #547.

Overlap with open PRs: none. No other open PR touches these ten files or their status lines.

There is one commit per function. Each adds the file and sets its status line. After those come the enum header commits, the review: commit and the merges of main.

Draft #525 stays open for its remaining functions.

🤖 Generated with Claude Code

https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp

gynt and others added 8 commits December 4, 2026 15:46
Skirmish::SkirmishLeaderboard_DeleteEntry. Text from draft sourcehold#525 (reimpl/mission-skirmish-flow), with the style pass from REIMPLEMENTATION_STYLE_GUIDE.md. Measured on main 46f21c5 with a full build of the eight files of this PR and reccmp: 100.0 / 100.0.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Skirmish::StoreGameIntoSKMasters. Text from draft sourcehold#525 (reimpl/mission-skirmish-flow), with the style pass from REIMPLEMENTATION_STYLE_GUIDE.md. Measured on main 46f21c5 with a full build of the eight files of this PR and reccmp: 100.0 / 100.0. The shift loop keeps `<= countof - 1`: with `< countof` MSVC compares against 250 with jge, the original against 249 with jg.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
… only the labels of two array[-1] addresses differ)

Skirmish::Skirmish_PrepareLeaderboardView. Text from draft sourcehold#525 (reimpl/mission-skirmish-flow), with the style pass from REIMPLEMENTATION_STYLE_GUIDE.md. Measured on main 46f21c5 with a full build of the eight files of this PR and reccmp: 99.1 / 97.6. The only differences are the symbol names reccmp gives two addresses just before an array (INT_ARRAY_00eb0e48 and INT_ARRAY_00ed2be0 indexed from -1): I<char>::instance and I<int>::instance in the original, a resolver instance and a resolver initializer in ours. Instructions, operands and offsets are the same.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Game::Tutorial_IsActionAllowed. Text from draft sourcehold#525 (reimpl/mission-skirmish-flow), with the style pass from REIMPLEMENTATION_STYLE_GUIDE.md. Measured on main 46f21c5 with a full build of the eight files of this PR and reccmp: 100.0 / 100.0.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
MapPropertiesState::adjustEventMonthAndYearForSection1047. Text from draft sourcehold#525 (reimpl/mission-skirmish-flow), with the style pass from REIMPLEMENTATION_STYLE_GUIDE.md. Measured on main 46f21c5 with a full build of the eight files of this PR and reccmp: 100.0 / 100.0.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
MapPropertiesState::loadMapSiegeHeaderForMissionIndex. Text from draft sourcehold#525 (reimpl/mission-skirmish-flow), with the style pass from REIMPLEMENTATION_STYLE_GUIDE.md. Measured on main 46f21c5 with a full build of the eight files of this PR and reccmp: 87.0 / 100.0. The raw difference is only the relative offsets of the four tail jumps to loadMapSiegeHeaderSections (0x004C3110). The header declares the mission number as char*; the file keeps that type and says so in a fixme.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
MapPropertiesState::loadMissionMapAndSetLord. Text from draft sourcehold#525 (reimpl/mission-skirmish-flow), with the style pass from REIMPLEMENTATION_STYLE_GUIDE.md. Measured on main 46f21c5 with a full build of the eight files of this PR and reccmp: 100.0 / 100.0.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Synchrony::LoadSkirmishCampaignData. Text from draft sourcehold#525 (reimpl/mission-skirmish-flow), with the style pass from REIMPLEMENTATION_STYLE_GUIDE.md. Measured on main 46f21c5 with a full build of the eight files of this PR and reccmp: 100.0 / 100.0. The two string copies are strcpy, the clear of the player names uses sizeof, and the Ghidra locals are named. Three loops stay do-while: as for-loops MSVC compiles the slot reset to a different loop and unrolls the two team loops, which the original does not.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

Style guide reading (advisory, posted once)

3 possible deviation(s) from REIMPLEMENTATION_STYLE_GUIDE.md across 1 of the 8 reimplemented .cpp file(s) in this PR.

The match comes first. Where the ugly form is what matched, it stays -- that is the guide's own overriding rule, and rule 21 asks for a one-line comment saying what was measured so the next reader does not retry it. This report is posted once, when the PR opens, and is not repeated on later pushes.

rule what it asks hits
9 Prefer for over while/do 3
Where

Rule 9 — Prefer for over while/do

  • src/OpenSHC/Synchrony/LoadSkirmishCampaignData.cpp:37 — do-loop; the guide asks for a for
  • src/OpenSHC/Synchrony/LoadSkirmishCampaignData.cpp:66 — do-loop; the guide asks for a for
  • src/OpenSHC/Synchrony/LoadSkirmishCampaignData.cpp:78 — do-loop; the guide asks for a for

gynt and others added 3 commits December 4, 2026 15:57
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
SkirmishLobbySetupStructure::restoreSkirmishLobbySetup. Text from draft sourcehold#525 (reimpl/mission-skirmish-flow), with the style pass from REIMPLEMENTATION_STYLE_GUIDE.md. Measured on main 592a17d with a full build of the ten files of this PR (plus IO/ResourceManager/paths.cpp as a callee for the link) and reccmp: 99.3 / 95.2. The only difference is the operand order of one byte store, mov byte ptr [eax + esi], dl in the original against [esi + eax] in ours: the same address, so no change in behaviour. The two unrolled copy loops are for-loops with countof; MSVC unrolls them as the original.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
MenuTextInputState::loadOrSaveGame. Text from draft sourcehold#525 (reimpl/mission-skirmish-flow), with the style pass from REIMPLEMENTATION_STYLE_GUIDE.md. Measured on main 592a17d with a full build of the ten files of this PR (plus IO/ResourceManager/paths.cpp as a callee for the link) and reccmp: 93.1 / 93.5. The original keeps the constant 16 in ebp and uses it for both std::string capacity checks and the fileListVisibleRowCount store; ours uses the immediate each time. The rest follows from that: the longer immediate encodings shift the relative jump offsets, the loop's alignment filler is a jmp over padding instead of lea esp, [esp], and the call and handler targets differ only as labels. No change in behaviour.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
@gynt gynt changed the title Reimplement 8 Skirmish and mission functions at 100% from #525 Reimplement 8 Skirmish and mission functions at 100% from #525, plus 2 with register allocation differences Dec 4, 2026
gynt and others added 4 commits December 4, 2026 17:12
The filter of the skirmish rankings (DAT_00ed3124 while the rankings are open). The values follow the buttons that set them in MenuItemActionHandler_RankingGames_Main: 1 is "Crusader Trail Only", 2 is "Custom Games Only", 0 is "All Games". Used by Skirmish_PrepareLeaderboardView.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
The sort column of the skirmish rankings (MissionDefinedData.sortColumn). Skirmish_PrepareLeaderboardView sorts column 2 by mission, 3 by game duration and 4 by date. Columns 0 and 1 both keep the stored order (by score), so they stay unknown for now.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
The action type that Tutorial_IsActionAllowed checks. Its callers on main pass 1 when a building is selected in the toolbar (with the building type), 3 when the rations change and 4 when the taxes change. 2 also takes a building type, but none of its callers is reimplemented yet.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
- Enum values instead of numbers: LeaderboardFilter and LeaderboardSortColumn in Skirmish_PrepareLeaderboardView, TutorialActionType and BuildingType in Tutorial_IsActionAllowed, MenuModalType (MMT_LOAD_MAP / MMT_SAVE_MAP) for loadOrSaveGame's action.
- TRUE/FALSE for 0/1 fields, and descending == TRUE for the BOOLEnum.
- countof bounds in LoadSkirmishCampaignData, and its narrowing casts are gone.
- The (short) casts in adjustEventMonthAndYearForSection1047 are gone.
- loadOrSaveGame: the save patterns come from string-macros.hpp with +=, and the queued command is (GameCommandType)50 instead of an OR of two unrelated values.
- #pragma warning(disable : 4996) for strcpy at the top of the three files that use it.
- Unused includes (INT_00ed2bdc, MSVC_SecurityCookie) removed.
- The code comments that explained forms kept for the match are gone; the PR thread explains them now. A comment that restated the code is gone too, and so is a speculative fixme on mission 18.

Re-measured with a full build and reccmp on main 592a17d: no score changes.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
@github-actions github-actions Bot added the declaration/type change This requires changes to the generated/exported types or definitions. label Dec 4, 2026
@gynt

gynt commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Some forms in this PR are kept only because they give the match. For each one, this lists the readable form I measured and what it cost. All numbers are from a full build and reccmp on main 592a17d (raw / normalized). The matching forms are at the scores in the PR table.

Kept for the match

  • StoreGameIntoSKMasters: the shift loop tests count <= countof(entries) - 1.
    • count < countof(entries) gives 96.9 / 90.9.
    • MSVC then compares against 250 with jge; the original compares against 249 with jg.
  • LoadSkirmishCampaignData: the slot reset and the two team loops stay do-while.
    • As for loops: 42.5 / 42.5. MSVC compiles the slot reset to a different loop and unrolls both team loops; the original does neither.
  • Skirmish_PrepareLeaderboardView: if (descending == TRUE) … else if (!descending).
    • if (descending) … else gives 90.6 / 87.3.
  • loadMapSiegeHeaderForMissionIndex and loadMissionMapAndSetLord: four identical branches (< 5, < 10, < 15, < 20).
    • A single < 20 test gives 75.0 / 75.0 and 35.7 / 35.7.
  • adjustEventMonthAndYearForSection1047: every mission branch stores yearCopy = year again, as the original does.
    • Dropping the repeated stores gives 40.0 / 40.0.
  • Tutorial_IsActionAllowed: (int)DAT_00df5590::instance < 4.
    • Without the cast: 99.0 / 95.2, because the compare becomes unsigned.
    • The original compares signed, so the global is probably an int, not unsigned int.

Header-side issues, not changed here (the generated headers stay as they are)

  • MapPropertiesState::loadMapSiegeHeaderForMissionIndex(char* param_1): the parameter is a mission number (int). The fixme: in the file stays for that.
  • MissionAestheticsDefinedData.field_0x3c … field_0x88: these are 20 map-name char* fields, typed char**.
    • That is why the code indexes them as (char*)(&field_0x3c)[i].
    • As an array of char* both the walk and the cast go away.
  • CampaignTrailMission.player1AI…, position1… and team1… are separate int fields. As arrays, the (&mission->team1)[i] walks in LoadSkirmishCampaignData go away.
  • Game::Tutorial_IsActionAllowed(undefined4 actionType, …) could take the new TutorialActionType.
  • MenuTextInputState::loadOrSaveGame(int action) could take MenuModalType; its callers already pass MMT_LOAD_MAP / MMT_SAVE_MAP.
  • SkMasterDataEntry.score holds the trail mission number (0 for a custom game), not a score: it is passed to the TEXT_TRAIL_NAMES_CRU lookup.
  • GameCommandType has no value 50 (it goes from 49 to 51). loadOrSaveGame queues (GameCommandType)50 until that value has a name.

No enum on main yet, values not confirmed

  • Rations 4 and taxes 7 in Tutorial_IsActionAllowed.
  • fileListContext 1 / 3 in loadOrSaveGame.
  • The -10 fill of playerPositionsArray in LoadSkirmishCampaignData.
  • Sort columns 0 and 1 are LBSC_UNKNOWN_0 / LBSC_UNKNOWN_1. Both keep the stored order, which is by score.

New enum headers in this PR: Game/Skirmish/Enums/LeaderBoardFilter.hpp, Game/Skirmish/Enums/LeaderBoardSortColumn.hpp and Game/Enums/TutorialActionType.hpp.

Casts kept after the cast pass. No cast in this PR could go without changing the code or the build. Kept:

  • loadMapSiegeHeaderForMissionIndex.cpp:17-34, the (int)missionNumber casts and the (char*)(&…field_0x3c)[…] casts: the header declares the parameter as char* and field_0x3c…0x88 as char**. The original uses an int mission number and a list of char* map names. I proposed both types for Ghidra (tested locally: every user keeps its score). The casts go away once the header types are fixed in Ghidra.
  • loadMissionMapAndSetLord.cpp:21-30, (char*)(&…field_0x3c)[…]: same field_0x3c header type.
  • LoadSkirmishCampaignData.cpp:35, (char*)mission->mapNameAddress: the field is declared pointer, but it is the map-name string. I proposed char*; the cast goes away once that is fixed in Ghidra.
  • Tutorial_IsActionAllowed.cpp:65,69, (int)DAT_00df5590::instance < 4: the header type is undefined4, but the original compares it signed. Without the cast the branch layout changes. I proposed int; the cast goes away once that is fixed in Ghidra.
  • loadOrSaveGame.cpp:55, (Commands::GameCommandType)50: needed to compile, because GameCommandType has no constant for 50.

gynt and others added 3 commits December 4, 2026 17:14
LF_ is already the prefix of Map/LogicHelpers/Logic1Flags (LF_NONE). Renamed in the header and in Skirmish_PrepareLeaderboardView; quick_diff output is unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
…Filter / LeaderBoardSortColumn

The type names now match their value prefixes: LBF_ for LeaderBoardFilter, and LBSC_ (unused on main) instead of LSC_ for LeaderBoardSortColumn. Files, generated path comments, the include and every use in Skirmish_PrepareLeaderboardView are renamed; quick_diff output is unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
…5-matched

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
@github-actions github-actions Bot removed the declaration/type change This requires changes to the generated/exported types or definitions. label Dec 4, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant